<?php
class Admin_PointController extends Zendhlt_Controller_AdminAction {

	const LIST_PAGE_TITLE = 'Danh sách điểm';
	const EDIT_PAGE_TITLE = 'Chỉnh sửa điểm';
	
	private $_currentUser;
	private $_arrParam;
	private $_paginator = array();
	private $_actionMain;
	private $_namespace;
	
	public function init() {
		$layoutPath = TEMPLATE_PATH . '/admin';
		$option = array('layout'     => 'index',
                   		'layoutPath' => $layoutPath);
		Zend_Layout::startMvc($option);

		// Phân trang
		$this->_paginator['itemCountPerPage'] = 5;
		$this->_paginator['pageRange'] 		  = 3;
		$this->_paginator['currentPage'] 	  = $this->_request->getParam('page', 1);

		// Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();

		// Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];

		// Dat ten SESSION
		// Luu cac du lieu filter vao SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);

		if(empty($ssFilter->col) || ($this->_request->getParam('resetfilter', 0) == 1)) {
			$ssFilter->keywords 	= '';
			$ssFilter->col 			= 'page_id';
			$ssFilter->order 		= 'ASC';
			$ssFilter->error 		= 0;
		}

		$this->_arrParam['paginator']			 = $this->_paginator;
		$this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 	 = $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 	 = $ssFilter->order;

		// Truyen ra view
		$this->view->actionMain = $this->_actionMain;
		$this->view->currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
		$this->view->arrParam = $this->_arrParam;

		// Lay thong tin user admin hien tai
		$auth = Zend_Auth::getInstance();
		$this->_currentUser = $auth->getIdentity();
	}
	
	public function indexAction() {
		$this->view->headTitle(self::LIST_PAGE_TITLE);
		
		$model = new Admin_Model_PointMapper();
		$paginator = new Zendhlt_Paginator();

		$points = $model->fetch($this->_arrParam);
		$totalItem = $model->countItem($this->_arrParam);

		$this->view->panigator = $paginator->createPaginator($totalItem, $this->_paginator);
		$this->view->totalItems = $totalItem;
		$this->view->Items = $points;
		
		// Display error if delete occur error {
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		$this->view->msg = '';
		
		if ($ssFilter->error == 1) {
			$this->view->msg = 'Xuất hiện lổi trong quá trình thực hiện!';
			$ssFilter->error = 0;
		}
		// }
	}
	
	public function filterAction() {
		$ssFilter = new Zend_Session_Namespace($this->_namespace);

		if ($this->_arrParam['type'] == 'search') {
			if($this->_arrParam['key'] == 1) {
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
			} else {
				$ssFilter->keywords = '';
			}
		}
		
		if ($this->_arrParam['type'] == 'order') {
			$ssFilter->col = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
		
		$this->_redirect($this->_actionMain . '/index');
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function editAction() {
		$this->view->headTitle(self::EDIT_PAGE_TITLE);

		$form = new Admin_Form_Point();
		$this->view->form = $form;
		$this->view->msg = '';
		
		$model = new Admin_Model_PageMapper();
		$pages = $model->fetchAll(null);
		foreach ($pages as $value) {
			$form->page_id->addMultiOption($value->getId(), $value->getPage());
		}
		
		$model = new Admin_Model_PositionMapper();
		$positions = $model->fetchAll(null);
		foreach ($positions as $value) {
			$form->position_id->addMultiOption($value->getId(), $value->getPosition());
		}

		$model = new Admin_Model_PointMapper();
		$arrData = $model->fetchAll('id=' . $this->_getParam('id', 0), 'edit');
		$data = array_shift($arrData);

		if ($data != null) {
			if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();

				if ($form->isValid($formData)) {
					$point = new Admin_Model_TableColumn_Point($data);

					$currentDate = date('Y-m-d H:i:s');
					$point->setPoint($form->getValue('point'));
					$point->setNguoi_cap_nhat($this->_currentUser->id);
					$point->setNgay_cap_nhat($currentDate);

					if ($model->save($point)) {
						$this->_redirect($this->_actionMain . '/index/page/' . $this->_paginator['currentPage']);
					} else {
						$this->view->msg = 'Xuất hiện lổi trong quá trình thực hiện.';
						$form->populate($formData);
					}
				} else {
					$form->populate($formData);
				}
			} else {
				$form->populate($data);
			}
		} else {
			$this->view->msg = 'Dữ liệu không tồn tại trong database.';
		}
	}
	
}
?>